package com.cs410.collabwriting.junit.main;

import java.util.ArrayList;
import java.util.Calendar;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import com.cs410.collabwriting.main.User;
import com.cs410.collabwriting.test.DatabaseUtility;
import com.cs410.collabwriting.tools.LogManager;

public class UserTest {

	@Rule
	public TestName m_testName = new TestName();
	private static User s_user;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		LogManager.logInfo("[JUnitTest] -- Starting JUnit Tests for User" );
		DatabaseUtility.DeleteAllTables();
		Thread.sleep(1000);
		DatabaseUtility.CreateAllTables();
		Thread.sleep(1000);
		DatabaseUtility.CreateAllTestingData();
		Thread.sleep(15000);
		
		s_user = new User("justine");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
//		DatabaseUtility.DeleteAllTables();
		LogManager.logInfo("[JUnitTest] -- Finished JUnit Tests for User" );
	}

	@Before
	public void setUp() throws Exception {
		LogManager.logInfo("[JUnitTest] -- Starting Test : " + m_testName.getMethodName() );
		s_user.update();
	}

	@After
	public void tearDown() throws Exception {
		LogManager.logInfo("[JUnitTest] -- Finished Test : " + m_testName.getMethodName() );
	}

	@Test
	public void userBiographyTest() {
		String biography = m_testName.getMethodName();
		boolean set = s_user.setBiography( biography );
		Assert.assertEquals( set, true );
		
		String returnedBio = s_user.getBiography();
		Assert.assertEquals( biography, returnedBio );
	}
	
	@Test
	public void userLocationTest() {
		String location = m_testName.getMethodName();
		boolean set = s_user.setLocation( location );
		Assert.assertEquals( set, true );
		
		String returnedLoc = s_user.getLocation();
		Assert.assertEquals( location, returnedLoc );
	}
	
	@Test
	public void userFirstNameTest() {
		String fname = m_testName.getMethodName();
		boolean set = s_user.setFirstName( fname );
		Assert.assertEquals( set, true );
		
		String returnedfname = s_user.getFirstName();
		Assert.assertEquals( fname, returnedfname );
	}
	
	@Test
	public void userLastNameTest() {
		String lname = m_testName.getMethodName();
		boolean set = s_user.setLastName( lname );
		Assert.assertEquals( set, true );
		
		String returnedlname = s_user.getLastName();
		Assert.assertEquals( lname, returnedlname );
	}

	@Test
	public void userGenderTest() {
		String gender = "MALE";
		boolean set = s_user.setGender( gender );
		Assert.assertEquals( set, true );
		
		String returnedGender = s_user.getGender();
		Assert.assertEquals( gender, returnedGender );
	}
	
	@Test
	public void userEmailTest() {
		String email = "123@321.com";
		boolean set = s_user.setEmailAddress( email );
		Assert.assertEquals( set, true );
		
		String returnedEmail = s_user.getEmailAddress();
		Assert.assertEquals( email, returnedEmail );
	}
	
	@Test
	public void userLastUpdatedTest() {
		s_user.update();
		s_user.getLastUpdate();
		long lastUpdated = 1234567;
		boolean set = s_user.setLastUpdated( lastUpdated );
		Assert.assertEquals( set, true );
		
		String updated = s_user.getLastUpdate();
		Assert.assertNotNull( updated );
		Assert.assertFalse( updated.isEmpty() );
	}
	
	@Test
	public void userPasswordTest() {
		String password = m_testName.getMethodName();
		boolean set = s_user.setPassword( password );
		Assert.assertEquals( set, true );
		
		String retPassword = s_user.getPassword();
		Assert.assertEquals( retPassword, password );
	}
	
	@Test
	public void userDateCreatedTest() {
		String dateCreated = s_user.getDateCreated();
		Assert.assertNotNull( dateCreated );
		Assert.assertFalse( dateCreated.isEmpty() );
	}
	
	@Test
	public void userIsActiveTest() {
		s_user.setLastUpdated( Calendar.getInstance().getTimeInMillis() );
		Assert.assertEquals( true , s_user.isActive() );
		
	}
	
	@Test
	public void getSubscriptionUpdates() {
		ArrayList<String[]> subUpdates = s_user.getSubscriptionUpdates();
		Assert.assertNotNull( subUpdates );
		Assert.assertTrue( subUpdates.size() > 0 );
		for( String[] update : subUpdates ) {
			Assert.assertNotNull( update );
			Assert.assertTrue( update.length > 0 );
		}
	}
	
	@Test
	public void activationKeyTest() {
	    Assert.assertTrue(s_user.setActivateKey(s_user.getEmailAddress()));
	    Assert.assertNotNull(s_user.getActivateKey());
	    Assert.assertTrue(s_user.activateUser(s_user.getUserID(), s_user.getActivateKey()));
	}
}
